package vNES;



import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class GameGenieDialog extends JDialog{
	
	NES nes;
	GameGenie gg;
	JTable tblCodes;
	JScrollPane codeScroll;
	MyTableModel model;
	
	public GameGenieDialog(GameGenie gg, NES nes){
		
		this.setTitle("vNES Game Genie");
		this.setSize(500,250);
		this.setModal(true);
		this.gg = gg;
		this.nes = nes;
		initComponents();
		
	}
	
	private void initComponents(){
		
		Container cp = getContentPane();
		
		model = new MyTableModel();
		tblCodes = new JTable(model);
		codeScroll = new JScrollPane(tblCodes);
		cp.setLayout(null);
		cp.add(codeScroll);
		codeScroll.setBounds(8,8,480,175);
		
	}
	
	
	private class MyTableModel extends AbstractTableModel{
		
		public int getColumnCount(){
			return 4;
		}
		
		public int getRowCount(){
			return gg.getCodeCount()+1;
		}
		
		public Object getValueAt(int row, int column){
			
			if(row<gg.getCodeCount()){
				switch(column){
					case 0:return gg.getCodeString(row).toUpperCase();
					case 1:return "$"+Misc.hex16(gg.getCodeAddress(row));
					case 2:return "$"+Misc.hex8(gg.getCodeValue(row));
					case 3:return "$"+Misc.hex8(gg.getCodeCompare(row));
					default: return "";
				}
			}else{
				return "";
			}
			
		}
		
		public String getColumnName(int column){
			switch(column){
				case 0: return "Code";
				case 1: return "Address";
				case 2: return "Value";
				case 3: return "Cmp";
				default:return "-";
			}
		}
		
		public boolean isCellEditable(int row, int col){
			return (col == 0);
		}
		
		public void setValueAt(Object val, int row, int col){
			
			if((val!=null && val instanceof String) && !((String)val).equals("")){
				String s = ((String)val).trim();
				if(s.length()==6 || s.length()==8){
					
					if(row<gg.getCodeCount()){
						gg.editCode(row,s);
					}else{
						gg.addCode(s);
					}
					
				}
			}else if(row<gg.getCodeCount()){
				gg.removeCode(row);
			}
			
			if(gg.getCodeCount()>0){
				// Enable game genie:
				//System.out.println("Game Genie enabled.");
				nes.setGameGenieState(true);
			}else{
				// Disable game genie:
				//System.out.println("Game Genie disabled.");
				nes.setGameGenieState(false);
			}
			
			if(val!=null){
				tblCodes.updateUI();
			}
			
		}
		
	}
	
	public void destroy(){
		
		nes = null;
		gg = null;
		
	}
	
}